from django import forms
from .models import Article
from .models import Mission
from .models import TaskRelation


class ArticleForm(forms.ModelForm):
    class Meta:
        model = Article
        fields = ['title', 'type', 'url', 'column', 'date']

class ContentItemForm(forms.Form):
    json_data = forms.CharField(widget=forms.Textarea, required=False)

ContentItemFormSet = forms.formset_factory(ContentItemForm, extra=1)

class MissionForm(forms.ModelForm):
    class Meta:
        model = Mission
        fields = ['institution', 'mission_name', 'launch_time', 'recommend','image_url', 'status', 'launch_time_utc']

# 如果forms.py中有TaskRelationForm，需要更新它以支持image_url字段
from django import forms
from .models import TaskRelation

class TaskRelationForm(forms.ModelForm):
    class Meta:
        model = TaskRelation
        fields = ['name', 'description', 'image_url', 'related_missions']

    def __init__(self, *args, **kwargs):
        super(TaskRelationForm, self).__init__(*args, **kwargs)
        # 按照 launch_time_utc 倒序排列 related_missions 的查询集
        if self.instance.pk:  # 确保实例已存在
            self.fields['related_missions'].queryset = Mission.objects.order_by('-launch_time_utc')
        else:
            # 如果是新建实例，默认显示所有任务（也可以根据需要调整）
            self.fields['related_missions'].queryset = Mission.objects.order_by('-launch_time_utc')